package cn.Alibaba.org.service.impl;

import cn.Alibaba.basic.exception.BusinessException;
import cn.Alibaba.basic.service.impl.BaseServiceImpl;
import cn.Alibaba.basic.util.BaiduAuditUtils;
import cn.Alibaba.org.domain.Employee;
import cn.Alibaba.org.domain.Shop;
import cn.Alibaba.org.dto.ShopDto;
import cn.Alibaba.org.mapper.EmployeeMapper;
import cn.Alibaba.org.mapper.ShopMapper;
import cn.Alibaba.org.service.IShopService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.List;


@Service
public class ShopServiceImpl extends BaseServiceImpl<Shop> implements IShopService {

    @Autowired
    private ShopMapper shopMapper;

    @Autowired
    private EmployeeMapper employeeMapper;


    @Transactional
    @Override
    public void save(Shop shop) {

       shopMapper.add(shop);
    }

    @Transactional
    @Override
    public void update(Shop shop) {
        shopMapper.update(shop);

    }

    @Override
    public void settlement(Shop shop) {

        if (StringUtils.isEmpty(shop.getName()) || StringUtils.isEmpty(shop.getTel()) || StringUtils.isEmpty(shop.getAddress())
                || StringUtils.isEmpty(shop.getAdmin().getPhone())
                || StringUtils.isEmpty(shop.getAdmin().getUsername())
                ||  StringUtils.isEmpty(shop.getAdmin().getEmail())
                || StringUtils.isEmpty(shop.getAdmin().getPassword())
                || StringUtils.isEmpty(shop.getAdmin().getComfirmPassword())

        ){
            throw new BusinessException("数据不能为空");

        }


        Boolean boo = BaiduAuditUtils.TextCensor(shop.getName());
        if (!boo){
            throw new BusinessException("店铺名称不合规");

        }

        Shop shopTem = shopMapper.loadByMessage(shop);

        if (shopTem!=null){

            throw new BusinessException("商家已经入驻，请直接登录，忘记密码请联系管理员");

        }




        // 获取employee对象
        Employee admin = shop.getAdmin();
        //
        employeeMapper.add(admin);

        // 将
        shop.setAdmin_id(admin.getId());
        shopMapper.add(shop);
        //2.3.将店铺的id更新到t_employee中
        admin.setShop_id(shop.getId());
        employeeMapper.update(admin);
    }

    @Override
    public List<ShopDto> getEcharts() {
        return shopMapper.getEcharts();
    }


}

